package com.vodafone.mapreduce.map;

import java.util.List;

import com.vodafone.mapreduce.dto.common.KeyValuePair;

/**
 * TODO: Insert your comments here!
 */
public class WordFrequencyCombineFunction extends CombineFunction<String, String, String, Integer> {

    @Override
    protected void doCombine(KeyValuePair<String, Integer> kvp) {
        List<Integer> combinedList = intermediate.get(kvp.getKey());
        if (combinedList.size() == 1) {
            Integer w = combinedList.remove(0);
            w += kvp.getValue();
            combinedList.add(w);
        } else if (combinedList.size() == 0) {
            throw new IllegalArgumentException("Something went wrong - Existing value list cannot be empty!");
        } else if (combinedList.size() > 1) {
            throw new IllegalArgumentException("Something went wrong - Existing value list cannot have more than one element!");
        }
    }

}
